package com.example.demo.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.service.ProxyReEncrypt;
import com.example.demo.service.PubParams;

import it.unisa.dia.gas.jpbc.Element;


@RestController
public class ProxyController {
	
	private ProxyReEncrypt  d=new ProxyReEncrypt ();
	@RequestMapping(value = "/content/{id}", method = { RequestMethod.GET })//Bob请求
	public Map getC(@PathVariable("id") Integer index) {
		try {
			String path="D:\\java_work\\data\\";
			byte[] c1=d.readFile(path+"c1");
			byte[] c2=d.readFile(path+"c2");
			byte[] c3=d.readFile(path+"c3");
			byte[] xbyte=d.readFile(path+"xbyte"+index.toString());
			byte[] ybyte=d.readFile(path+"ybyte"+index.toString());
			byte[] rkw_1byte=d.readFile(path+"rkw_1byte");
			PubParams p=null;
			try {
				p=d.read();// IO
			} catch (IOException e) {
				e.printStackTrace();
			}
			Element rkw_1=p.G2.newElementFromBytes(rkw_1byte);
			Element c2Element=p.Zr.newElementFromBytes(c2);
			Element c21= rkw_1.duplicate().mulZn(c2Element);
			Element c31=p.G2.newElementFromBytes(c3).duplicate().mulZn(p.Zr.newElementFromBytes(ybyte).duplicate());
			Map<String,String> C=new HashMap<>();
			C.put("c1", new String(c1,"ISO-8859-1"));
			C.put("c21", new String(c21.toBytes(),"ISO-8859-1"));
			C.put("c31", new String(c31.toBytes(),"ISO-8859-1"));
			C.put("x", new String(xbyte,"ISO-8859-1"));
			 return C;
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return null;
	}

}
